package data

import (
	"blog/internal/biz"
	"context"
	"github.com/go-kratos/kratos/v2/log"
	"time"
)

// 定义数据表结构体
type Article struct {
	ID        int64     `gorm:"primarykey"`
	Title     string    `gorm:"column:title;type:varchar(255);comment '标题'"`
	Content   string    `gorm:"column:content;type:text;comment '内容'"`
	Like      int64     `gorm:"column:like;type:bigint;comment '点赞量'"`
	CreatedAt time.Time `gorm:"column:add_time"`
	UpdatedAt time.Time `gorm:"column:update_time"`
}

type articleRepo struct {
	data *Data
	log  *log.Helper
}

func (ar *articleRepo) ListArticle(ctx context.Context) ([]*biz.Article, error) {
	var articleList []Article
	articles := make([]*biz.Article, 0)
	if err := ar.data.db.Find(&articleList).Error; err != nil {
		return nil, err
	}
	for _, v := range articleList {
		articles = append(articles, &biz.Article{
			ID:        v.ID,
			Title:     v.Title,
			Content:   v.Content,
			CreatedAt: v.CreatedAt,
			UpdatedAt: v.UpdatedAt,
			Like:      v.Like,
		})
	}
	return articles, nil
}

func (ar *articleRepo) GetArticle(ctx context.Context, id int64) (*biz.Article, error) {
	//TODO implement me
	panic("implement me")
}

func (ar *articleRepo) CreateArticle(ctx context.Context, article *biz.Article) error {
	//TODO implement me
	panic("implement me")
}

func (ar *articleRepo) UpdateArticle(ctx context.Context, id int64, article *biz.Article) error {
	//TODO implement me
	panic("implement me")
}

func (ar *articleRepo) DeleteArticle(ctx context.Context, id int64) error {
	//TODO implement me
	panic("implement me")
}

// NewArticleRepo . 这里需要注意，上面 data 文件 wire 注入的是此方法，方法名不要写错了
func NewArticleRepo(data *Data, logger log.Logger) biz.ArticleRepo {
	return &articleRepo{
		data: data,
		log:  log.NewHelper(logger),
	}
}
